<?php
/**
 * Created by PhpStorm.
 * User: ���ǿ
 * Date: 2017/3/20
 * Time: 21:47
 */

namespace Admin\Controller;


use Common\Controller\BaseController;
use Common\Model\GoodsModel;

class GoodsController  extends BaseController
{ protected $db;
   public function __init()
   {

       $this->db = new GoodsModel();
   }
    public function index()
    {
       $goods =  m('goods')->select();
        //dd($goods);
        $this->assign('goods',$goods);
        $this->display();
    }

    public function add()
    {

      if(IS_POST)
      {
          if(IS_POST){
             $data = I('post.');
//              dd($data);die;
              $res = $this->db->store($data);
              if($res['valid']){
                  //说明成功
                  $this->success($res['msg'],u('index'));exit;
              }else{
                  //说明添加失败
                  $this->error($res['msg']);exit;
              }
          }

      }

     //1.接收分类数据[实例化模型调用getCate的方法]
     $cateData =  (new GoodsModel())->getCate();
     //(2).分配变量
       // dd($cateData);
     $this->assign('cateData',$cateData);
     //2.接收品牌数据实例化模型调用getBrand方法
     $brandData = (new GoodsModel())->getBrand();
     //(2).分配变量
       // dd($brandData);
     $this->assign('brandData',$brandData);


     //显示模板
     $this->display();
    }

    public function ajaxGetSpec()
    {
        if(IS_AJAX)
        {
            //1.接受tid
//            $tid = I('post.tid');


            //2.根据tid在类型属性表中获取数据
            $data = m('typeattr')->where("tid=".I('post.tid'))->select();
            //3.将数据中每一条的属性值【value】变为数组
            foreach ($data as $k=>$v){
                $data[$k]['value'] = explode(',',$v['value']);
                //把字符串分割成数组
                $data[$k]['typeattr']=explode(',',$data[$k]['typeattr']);
            }
            // dd($data);
            //4.将数据返回
            $this->ajaxReturn($data);
        }

    }

    /**
     * 编辑商品
     */
    public function edit()
    {
        $gid = I('get.gid');
        if(IS_POST)
        {
//            dd($_POST);die;
            $re = (new GoodsModel())->edit($_POST,$gid);
            if($re['valid']){
                //说明成功
                $this->success($re['msg'],u('index'));exit;
            }else{
                //说明添加失败
                $this->error($re['msg']);exit;
            }
        }
        //1.接收分类数据[实例化模型调用getCate的方法]
        $cateData =  (new GoodsModel())->getCate();
        //分配变量
        // dd($cateData);
        $this->assign('cateData',$cateData);
        //2.接收品牌数据实例化模型调用getBrand方法
        $brandData = (new GoodsModel())->getBrand();
        //分配变量
//         dd($brandData);
        $this->assign('brandData',$brandData);


        //3.接收旧数据将商品表与商品详细表关联 商品属性表需要单独处理
//        方法一 关联数据
//        $oldData = M('goods')
//            ->join('shop_detail ON shop_goods.gid = shop_detail.gid')
//            ->where("shop_goods.gid={$gid}")->find();
        //方法二 起别名关联
            $oldData = M('goods')->alias('g')
            ->join('__DETAIL__ d ON g.gid = d.gid')
            ->where("g.gid={$gid}")->find();
//        dd($oldData);die;
            $this->assign('oldData',$oldData);


        //4.获取属性值
        //查找当前列表的属性值 因为之前定义当value=1 时是属性 =0时是规格
        $attrData =  m('typeattr')->where("tid={$oldData['tid']} and value=1")->select();
        //dd($attrData);
        $this->assign('attrData',$attrData);
        //将数据中每一条的属性值【value】变为数组
        foreach ($attrData as $k=>$v){
            $attrData[$k]['value'] = explode(',',$v['value']);
            //把字符串分割成数组
            $attrData[$k]['typeattr']=explode(',',$attrData[$k]['typeattr']);

        }
        //dd( $attrData);
        //商品属性表中获取当前商品所有规格属性
        $hasAttr = m('goods_attr')->where("gid=$gid")->getField('gtvalue',true);
        //dd($hasAttr);
        $this->assign('attrData',$attrData);
        $this->assign('hasAttr',$hasAttr);


        //5.获取规格值
        $specData = m('typeattr')->alias('ta')
            ->join("__GOODS_ATTR__ ga on ta.taid = ga.taid")
            ->where("ta.value = 0 and ga.gid = $gid")->select();
       // dd($specData);
        foreach($specData as $k=>$v)
        {
            $specData[$k]['typeattr'] = explode(',',$v['typeattr']);
        }
        $this->assign('specData',$specData);
        //dd( $specData);


        $this->display();

    }

    /**
     * 删除方法
     */
    public function del()
    {
       $gid = I('get.gid');

        //删除列表图
        $picData = m('goods')->where("gid='{$gid}'")->field("drawing")->find();
        if(file_exists($picData['drawing'])){
            unlink($picData['drawing']);
        }
        //删除服务器多图
        $imgData = m('detail')->where("gid='{$gid}'")->field("small,big,medium")->find();
        $newImgData = [];
        foreach ($imgData as $k=>$v){
            $newImgData[] = explode('|',$v);
        }
        foreach ($newImgData as $k=>$v){
            foreach ($v as $kk=>$vv){
                if(file_exists($vv)){
                    unlink($vv);
                }
            }
        }

        //删除属性表 详细表 商品表字段
       m('goods')->where("gid='{$gid}'")->delete();
       m('detail')->where("gid ='{$gid}'")->delete();
       m('goods_attr')->where("gid='{$gid}'")->delete();
        $this->success('删除成功',u('index'));





    }
}




